from flask import g, Blueprint, request, jsonify, render_template, redirect
from sqlalchemy import and_

from common import Result
from forms import AddStudentForm
from models import User, Root, Teacher, Student, Classes, TestPaper, StudentClassesRel, SingleChoiceQuestionBank, \
    ShortAnswerQuestionBank, JudgmentalQuestionBank, PictureQuestionBank, SignIn
from exts import db, get_time
from flask_login import login_required
from decorations import root_required

bp = Blueprint("api_admin", __name__, url_prefix="/api/admin")


# todo before_request
@bp.before_request
@login_required
@root_required
def before_request():
    pass


@bp.route("/delete_student", methods=["POST"])
def delete_student():
    id_user = request.form.get("id_user")
    # print(id_user)
    u = db.session.query(User).filter(and_(
        User.is_delete == 0,
        User.id == int(id_user)
    )).first()
    if u is None:
        return Result.error(
            msg="已经注销过了！"
        )
    u.is_delete = 1

    db.session.commit()

    return Result.success(
        msg="注销成功"
    )


@bp.route("/recovery_student", methods=["POST"])
def recovery_student():
    id_user = request.form.get("id_user")
    # print(id_user)
    u = db.session.query(User).filter(and_(
        User.is_delete == 1,
        User.id == int(id_user)
    )).first()
    if u is None:
        return Result.error(
            msg="账号已在用！"
        )
    u.is_delete = 0

    db.session.commit()

    return Result.success(
        msg="恢复成功"
    )
